package ro.sergiu.finder.ui.button;

import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import ro.sergiu.finder.models.finder.FinderModel;
import ro.sergiu.finder.models.finder.db.DbFinderModel;
import ro.sergiu.finder.models.frame.FrameModel;
import ro.sergiu.finder.models.grid.GridModel;
import ro.sergiu.finder.models.property.DisplayMode;
import ro.sergiu.finder.ui.FindDialog;
import ro.sergiu.finder.ui.button.listeners.SelectionListener;

/**
 *
 * @author toporjinschi
 * @param <T>
 */
public class ButtonFinder<T> extends JButton {

    private FinderModel finderModel = null;
    private FrameModel frameModel = new FrameModel();
    private GridModel gridModel = new GridModel();

    public ButtonFinder() {
        initComponents();
    }

    //<editor-fold defaultstate="collapsed" desc="WindowProperties">
    public FinderModel getFinderModel() {
        return finderModel;
    }

    public void setFinderModel(DbFinderModel finderModel) {
        this.finderModel = finderModel;
    }

    public FrameModel getWindowSettings() {
        return frameModel;
    }

    public void setWindowSettings(FrameModel windowSettings) {
        this.frameModel = windowSettings;
    }

    public GridModel getTableSettings() {
        return gridModel;
    }

    public void setTableSettings(GridModel tableSettings) {
        this.gridModel = tableSettings;
    }

    //</editor-fold>
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setIcon(new javax.swing.ImageIcon(getClass().getResource("/ro/sergiu/finder/resources/search.png"))); // NOI18N
        setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        setPreferredSize(new java.awt.Dimension(80, 24));
        addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formActionPerformed(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_formActionPerformed
        FindDialog<T> fd = new FindDialog<T>(finderModel, gridModel);
        if (frameModel.getDisplayMode() == DisplayMode.MODAL_DIALOG) {
            customizeDialog(fd);
        } else {
            throw new UnsupportedOperationException("Not yet supported!"); //TODO popup
        }
        fd.setVisible(true);
        fireSelectionEvent(fd.getSelectedObjects());
    }//GEN-LAST:event_formActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private void customizeDialog(FindDialog<T> fd) {
        String title = frameModel.getTitle();
        fd.setIconImage(((ImageIcon) this.getIcon()).getImage());
        fd.setTitle(title == null ? this.getText() : title);
        fd.setResizable(frameModel.isResizable());
        fd.setSize(frameModel.getSize());
        fd.setPreferredSize(frameModel.getPreferredSize());
        fd.setMaximumSize(frameModel.getMaximumSize());
        fd.setMinimumSize(frameModel.getMinimumSize());
    }

    //<editor-fold defaultstate="collapsed" desc="Selection listener methods">
    public final void addSelectionListener(SelectionListener listener) {
        this.listenerList.add(SelectionListener.class, listener);
    }

    public final void removeSelectionListener(SelectionListener listener) {
        this.listenerList.remove(SelectionListener.class, listener);
    }

    private void fireSelectionEvent(List<T> selection) {
        Object[] listeners = this.listenerList.getListenerList();
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == SelectionListener.class) {
                ((SelectionListener) listeners[i + 1]).perform(selection);
            }
        }
    }
    //</editor-fold>
}
